package servlet;

import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Study;
import DAO.StudyDao;
import DAO.UserDao;
import servlet.Util;

public class MemberLoginAction implements Action {

	@Override
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		HttpSession session=request.getSession();
		ActionForward forward=null;
		
		UserDao userDao = new UserDao();
		
		String memid=request.getParameter("memid");
		String passwd=request.getParameter("passwd");
		
		


		boolean check=userDao.isLogin(memid, passwd);
		
		
		if(check){
			session.setAttribute("memid", memid);
			String userId = userDao.getUserIdByMemId(memid);
			session.setAttribute("userid", userId);
			
			ArrayList<Integer> studyId = new ArrayList<Integer>();
			studyId = userDao.getUserJoinedStudyInfo(Integer.parseInt(userId));
			
			ArrayList<Study> studyList = new ArrayList<Study>();
			StudyDao studyDao = new StudyDao();
			
			for(int i=0; i<studyId.size(); i++)
			{
				studyList.add(studyDao.getStudyInfo(studyId.get(i)));
			}
			
				session.setAttribute("studyList", studyList);	
			
			
			Util.ShowSuccess(response, "로그인이 완료되었습니다.", "./questudy.jsp");
		
		}
		
		else{
			Util.ShowError(response, "아이디 또는 비밀번호를 다시 확인해주세요.");
		}
		
		return null;
	
	}

}
